package com.fangzhi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fangzhi.mapper.AdminAccountRoleDeptRelationMapper;
import com.fangzhi.mobel.AdminAccountInfo;
import com.fangzhi.mobel.AdminAccountRoleDeptRelation;
import com.fangzhi.mobel.enums.GlobalCodeEnum;
import com.fangzhi.mobel.io.CreateUserIo;
import com.fangzhi.mobel.vo.*;
import com.fangzhi.service.AdminAccountInfoService;
import com.fangzhi.mapper.AdminAccountInfoMapper;
import com.fangzhi.utils.AESUtil;
import com.fangzhi.utils.BeanCopyUtil;
import com.fangzhi.utils.GlobalParam;
import com.fangzhi.utils.ResponseResult;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author edy
* @description 针对表【admin_account_info】的数据库操作Service实现
* @createDate 2024-09-10 10:00:22
*/
@Service
public class AdminAccountInfoServiceImpl extends ServiceImpl<AdminAccountInfoMapper, AdminAccountInfo>
    implements AdminAccountInfoService{

    @Autowired
    private AdminAccountRoleDeptRelationMapper adminAccountRoleDeptRelationMapper;


    @Override
    @Transactional
    public ResponseResult updateUser(CreateUserIo io) {
        // 删除修改前的角色和部门关联
        adminAccountRoleDeptRelationMapper.delete(Wrappers.<AdminAccountRoleDeptRelation>lambdaQuery()
                .eq(AdminAccountRoleDeptRelation::getAdminAccounntId, io.getId()));

        AdminAccountInfo adminAccountInfo = this.getBaseMapper().selectById(io.getId());
        if (null == adminAccountInfo)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9996);

        String decrypt = AESUtil.decrypt(io.getPassword());
        adminAccountInfo.setName(io.getName()).setNickName(io.getNickName()).setMobile(io.getMobile())
                .setGender(io.getGender()).setAvatar(io.getAvatar()).setStatus(io.getStatus()).setUserCode(null)
                .setPassword(decrypt).setJoinTime(io.getJoinTime()).setWxNum(io.getWxNum()).setUserCode(io.getUserCode());
        int update = this.getBaseMapper().updateById(adminAccountInfo);
        if (update != 1)
            throw new RuntimeException("修改员工失败" + JSON.toJSONString(io));

        int insert = adminAccountRoleDeptRelationMapper.insert(new AdminAccountRoleDeptRelation()
                .setAdminAccounntId(io.getId().longValue()).setRoleId(io.getRoleId().longValue())
                .setDeptId(io.getDeptId().longValue()));
        if (insert != 1)
            throw new RuntimeException("修改员工失败" + JSON.toJSONString(io));
        return ResponseResult.OK();

    }

    @Override
    public ResponseResult saveUser(CreateUserIo io) {
        boolean b = checkUserCode(io.getUserCode());
        if (b)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9995);

        AdminAccountInfo adminAccountInfo = BeanCopyUtil.copyProperties(io, AdminAccountInfo.class);
        String decrypt = AESUtil.decrypt(io.getPassword());
        if (null == decrypt)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9996);

        adminAccountInfo.setPassword(decrypt);
        int insert = this.getBaseMapper().insert(adminAccountInfo);
        if (insert != 1)
            throw new RuntimeException("新增员工失败" + JSON.toJSONString(io));

        insert = adminAccountRoleDeptRelationMapper.insert(new AdminAccountRoleDeptRelation()
                .setAdminAccounntId(adminAccountInfo.getId()).setRoleId(io.getRoleId().longValue())
                .setDeptId(io.getDeptId().longValue()));
        if (insert != 1)
            throw new RuntimeException("新增员工失败" + JSON.toJSONString(io));
        return ResponseResult.OK();
    }

    @Override
    public ResponseResult userList(List<Long> roleIds, List<Long> deptIds, Integer accountId, Integer pageNo, Integer pageSize) {

        Long total = this.getBaseMapper().userListCount(roleIds, deptIds, accountId);
        if (total == 0)
            return ResponseResult.OK(new PageVo().setTotal(0L).setData(new ArrayList<>()));

        List<UserListVo> voList = this.getBaseMapper().userList(roleIds, deptIds, accountId, (pageNo - 1) * pageSize, pageSize);
        voList.parallelStream().forEach(T->{
            T.setStatusButton(false);
            if (T.getStatus() == 1)
                T.setStatusButton(true);
        });
        return ResponseResult.OK(new PageVo().setTotal(total).setData(voList));
    }

    @Override
    public List<Long> accountList(List<Long> roleIds, List<Long> deptIds, Integer accountId) {
        return this.getBaseMapper().selectAccountListByDeptByRoleByAccountId(roleIds, deptIds, accountId);
    }

    @Override
    public List<CommissionTaskNewVo> queryCommission() {
        return this.getBaseMapper().queryCommission();
    }

    public boolean checkUserCode(String userCode){
        AdminAccountInfo adminAccountInfo = this.getBaseMapper().selectOne(Wrappers.<AdminAccountInfo>lambdaQuery()
                .eq(AdminAccountInfo::getUserCode, userCode));
        return null != adminAccountInfo;
    }
}




